layui.use(['jquery',"index","echarts"], function () {//,'echarts'
   // var echarts = layui.echarts;
    let table = layui.table;


   //页面每5分钟定时刷新
  /*  setInterval(function () {
        //客户汇总、供应商汇总
        //byxzkh();
    },1000*60*5);*/

    //客户汇总、供应商汇总
    byxzkh();

    getNowMonth()
    getEmailNow()
    getBillingNow();
//获取外销相关信息
    getContractInfo();
    let initData = [];
    table.render({
        elem: '#exportcontract'
        ,data:initData
        ,url:'exportContract/statistics/getStatisticsExportInfoByPage'
        ,skin:'line'
        ,cols: [[
            {field:'billNo', title:'外销合同信息'}
            ,{field:'purchaseNo', title:'采购单号'}
            ,{field:'productCode', title:'产品编码'}
            ,{field:'deliveryDate', title:'采购交期'}
            ,{field:'feedbackDeliveryDate', title:'供应商反馈交期'}
            ,{field:'actualDeliveryDate', title:'实际交期'}
            ,{field:'productNum', title:'数量'}
            ,{field:'memo', title:'备注'}
            ,{fixed: 'right',toolbar: '#exportcontracteditor', title:'操作'}
        ]]
        ,id: 'exportcontract'
        ,height: 555
        ,page: true
    });
    table.on('tool(exportcontract)', function(obj){
        if(obj.event === 'view'){
            parent.intab("views/jsp/ExportBusiness/tpl/viewexport.jsp?id="+obj.data.id,"查看合同明细")
        }
    });
    //excel导出
    /*form.on('submit(exportData)', function () {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            url:"exportContract/export/getStatisticsExportInfo",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '外销合同交期详情.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });*/



    //审批中心
    table.render({
        elem: '#examination'
        ,typ:"get"
        ,url:"process/queryToDoTasksCount"
        ,skin:'line'
        ,cols: [[
            {field:'sptype', title:'审批类型'}
            ,{field:'spcount', title:'审批条数'}
            ,{fixed: 'right',toolbar: '#examinationeditor', title:'操作'}
        ]]
        ,id: 'examination'
        ,height: 500
    });
    //查询审批
    table.on('tool(examination)', function(obj){
        var data = obj.data;
        if(obj.event === 'view'){
            parent.intab("views/jsp/SystemCenter/tpl/todo.jsp?processKey="+data.processKey,"待办事项")
        }
    });

    //客户汇总、供应商汇总
   function byxzkh(){

       $.ajax({
           type : 'get',
           //  url : 'resource/getTree',
           //data:{"ids":JSON.stringify(ids)},
           url:'customer/getByxzkh',
           dataType:"json",
           /*  contentType: 'application/json',*/
           async:true,
           success : function (da) {

               if(da.status==200 ){

                   setCustomer(da.data);
               }
           },
           error: function(data) {
               // layer.msg("获取失败!");
           }
       });

   }
   //客户、供应商赋值
   function setCustomer(data){
       //30天未联系
       var contact30Count=data.contact30Count;
       //60天未联系
       var contact60Count=data.contact60Count;
       //85天未联系
       var contact85Count=data.contact85Count;
       //本月新增
       var newCustomerCount=data.newCustomerCount;
       $("#byxzkh")[0].textContent=newCustomerCount;
       $("#wlx30")[0].textContent=contact30Count;
        $("#wlx60")[0].textContent=contact60Count;
       $("#wlx85")[0].textContent=contact85Count;
        //供应商
       var suplierType1=data.suplierType1;
       var suplierType2=data.suplierType2;
       var suplierType3=data.suplierType3;
       var suplierType4=data.suplierType4;
       if($("#suplierType4").length >0){
           $("#suplierType4")[0].textContent=suplierType4;
       }
       if($("#suplierType3").length >0){
           $("#suplierType3")[0].textContent=suplierType3;
       }
       if($("#suplierType2").length >0){
           $("#suplierType2")[0].textContent=suplierType2;
       }
       if($("#suplierType1").length >0){
           $("#suplierType1")[0].textContent=suplierType1;
       }




   }
    let option = {
        color: ['#009688','#2f4554', '#61a0a8'],
        title: {
            text: '1月'
        },
        tooltip: {},
        legend: {
            data: ['外销']
        },
        xAxis: {

            type: "value",

        },
        yAxis: {
            type: "category",
            data: ["业务员1", "业务员2", "业务员3", "业务员4", "业务员5", "业务员6"]

        },
        series: [{
            name: '外销',
            type: 'bar',
            itemStyle:{
                normal:{
                    label: {
                        show: true,
                        position: 'inside',
                        textStyle:{
                            color:"black",
                            fontSize:14
                        }
                    },
                }
            },

            data: [200, 500, 1500, 2000, 3500, 4500]
        }]
    };

    //获取dom容器
    let myChart1 = layui.echarts.init(document.getElementById('zzt'));

    // 使用刚指定的配置项和数据显示图表。
    myChart1.setOption(option);

    function  zkqp() {
        var index = layer.open({
            type: 2,
            title: '全屏数据',
            content: 'views/jsp/SystemCenter/tpl/statisticalfigure.jsp',
            area: ['100%', '100%'],
            maxmin: true
        });
        layer.full(index);
    }

    let startDate = 0;
    let endDate = 0;
    function getNowMonth() {
        $.ajax({
            async:true,
            type: 'POST',
            url: 'number/getNowMonthNew',
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            success: function (res) {
                if (res.status == 200) {
                    if (res.data != "") {
                        startDate = res.data.startDate;
                        endDate = res.data.endDate;
                        getComplaintCount();
                        getBlueprintCount();
                    }
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            },
            error: function (data) {
                layer.msg(data.msg, {icon: 5});
            }
        });
    }


    function getComplaintCount() {
        $.ajax({
            type: 'POST',
            url: 'complaint/selectList',
            data:JSON.stringify({"startCreateTime":new Date(startDate),"endCreateTime":new Date(endDate)}),
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            success: function (res) {
                $("#ksNum").text(res.total);
            },
            error: function (data) {
                // layer.msg(data.msg, {icon: 5});
            }
        });
    }

    function getBlueprintCount() {
        $.ajax({
            type: 'POST',
            url: 'blueprint/selectBlueprintList',
            data:JSON.stringify({"startCreateTime":new Date(startDate),"endCreateTime":new Date(endDate)}),
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            success: function (res) {
                if (res.status == 200) {
                    if (res.data != "") {
                        $("#tzNum").text(res.data.length);
                    }
                } else {
                    // layer.msg(res.msg, {icon: 5});
                }
            },
            error: function (data) {
                // layer.msg(data.msg, {icon: 5});
            }
        });
    }

//工作台邮件汇总
    function getEmailNow() {
        $.ajax({
            type: 'GET',
            url: 'email/getNowEmail',
            success: function (res) {
                var s=res.data;
                var date=s.split(";")
                //"今日收件："+pnow+";今日发件："+snow+";今日未读邮件+"+ppnow
                var  pnow=date[0];
                var  snow=date[1];
                var ppnow=date[2];
                $("#nowGetEmail").append("<cite>"+pnow+"</cite>")
                $("#nowSendEmail").append("<cite>"+snow+"</cite>")
                $("#nowGetEmailNo").append("<cite>"+ppnow+"</cite>")
            },
        });
    }
// getComplaintCount();
// getBlueprintCount();



//获取当月开票数量
    function getBillingNow(){
        let dateTime=dateStr();
        let startCreateDate=dateTime.startDate;
        let endCreateDate=dateTime.endDate;
        $.ajax({
            url: 'billing/findBillingSize',
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {
                "startCreateDate":startCreateDate,
                "endCreateDate":endCreateDate
            },
            async: true,
            success: function (res) {
                if (res.status == 200) {
                    let num=res.data;
                    $("#billNum").append("<cite>"+num+"</cite>")

                    let billA=$("#billA").attr("lay-href");
                    billA=billA+'?startCreateDate='+startCreateDate+'&endCreateDate='+endCreateDate;
                    $("#billA").attr("lay-href",billA);
                }
            }
        });
    }
//获取当月时间
    function dateStr(){
        let dateTime={};
        var nowdays = new Date();
        var year = nowdays.getFullYear();
        var month = nowdays.getMonth()+1;
        if (month == 0) {
            month = 12;
            year = year - 1;
        }
        if (month < 10) {
            month = '0' + month;
        }
        var myDate = new Date(year, month, 0);
        dateTime['startDate'] = year + '-' + month + '-' + 1; //当前月第一天
        dateTime['endDate'] = year + '-' + month + '-' + myDate.getDate(); //当前月最后一天
        return dateTime;
    }



//获取外销相关信息
    function getContractInfo() {
        //本月备货合同数量
        $.ajax({
            url:"task/statistics/getTaskCount",
            success:function(data){
                if(data.status == 200){
                    $("#taskBillNoThisMonth")[0].textContent = data.data;
                }
            }
        });

        //本月出运单据
        $.ajax({
            url:"carryDetail/statistics/getCarryBillNoCount",
            success:function(data){
                if(data.status == 200){
                    $("#carryBillNoThisMonth")[0].textContent = data.data;
                }
            }
        });

        //本月待认领水单
        $.ajax({
            url:"receivePayment/statistics/getBillNoCount",
            success:function(data){
                if(data.status == 200){
                    $("#receivePaymentBillNoThisMonth")[0].textContent = data.data;
                }
            }
        });

        //本月外销合同数量
        $.ajax({
            url:"exportContract/statistics/getExportBillNoCount",
            success:function(data){
                if(data.status == 200){
                    $("#exportBillNoThisMonth")[0].textContent = data.data;
                }
            }
        });

        //本月外销合同总额USD
        $.ajax({
            url:"exportContract/statistics/getTotalContractAmount",
            success:function(data){
                if(data.status == 200){
                    $("#exportUSDTotalContractAmountThisMonth")[0].textContent = data.data;
                }
            }
        });

        //本月报价成交率
        $.ajax({
            url:"exportContract/statistics/getOfferTurnoverRate",
            success:function(data){
                if(data.status == 200){
                    $("#offerTurnoverRateThisMonth")[0].textContent = data.data + "%";
                }
            }
        });

        //本月报价总额USD
        $.ajax({
            url:"offer/statistics/getOfferTotalContractAmount",
            success:function(data){
                if(data.status == 200){
                    $("#offerUSDTotalContractAmountThisMonth")[0].textContent = data.data;
                }
            }
        });
    }

});
